package wc.fileUtil;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import wc.fileUtil.exception.NoReadableFileException;

public class LibFileReader extends FileReader {
	
	static private Pattern _p = Pattern.compile("<dd>&nbsp;&nbsp; (.+)");
	static private Pattern _tag = Pattern.compile("<.+>");
	static private Pattern _screening = Pattern.compile("&[a-zA-Z]+;");
	
	public LibFileReader(String encode) {
		super(encode);
	}

	@Override
	public void parseFile(File file) throws NoReadableFileException {
		clear();
		if(file.isDirectory()) throw new NoReadableFileException();
        String s;
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), getEncode()));
            while((s = br.readLine()) != null) {
            	Matcher m = _p.matcher(s);
            	if(m.find()) {
            		String line = m.group(1);
            		Matcher tag = _tag.matcher(line);
            		if(tag.find()) {
            			line = line.replace(line.substring(tag.start(0), tag.end(0)), "");
            		}
            		Matcher screening = _screening.matcher(line);
            		if(screening.find()) {
            			line = line.replace(line.substring(screening.start(0), screening.end(0)), "");
            		}
            		addLine(line);
            	}
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
	}

}
